const fetch = require('./fetch.js')
let cookie = {}
let info = null
const login = () => {
  return new Promise((resolve, reject) => {
    if (info) {
      resolve(info)
    } else {
      let app = getApp()
      wx.login({
        success (res) {
          fetch.post(app.globalData.config._domain + '/miniapp-user/login', {
            code: res.code,
            applyid: 7
          }).then(res => {
            info = res.data.data
            fetch.setCookie(String(res.header['Set-Cookie']))
            // cookie.cookie = String(res.header['Set-Cookie'])
            resolve(res.data.data)
            app.globalData.unionid = info.unionid
          })
        },
        fail (res) {
          reject(res)
          wx.showToast({
            title: '登录失败',
            icon: 'none'
          })
        }
      })
    }
  })
}
export {login, cookie}
